//
//  CoolInputAccessoryView.m
//  CoolFramework
//
//  Created by Felix on 13-3-21.
//  Copyright (c) 2013年 FelixApp. All rights reserved.
//

#import "CoolInputAccessoryView.h"

@implementation CoolInputAccessoryView {
    __weak UIResponder  *responder_;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithTextFieldOrTextView:(UIResponder*)textFieldOrTextView {
    self = [self initWithFrame:CGRectMake(0.f, 0.f, 100.f, 44.f)];
    if (self) {
        responder_ = textFieldOrTextView;
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
        toolbar.backgroundColor = [UIColor whiteColor];
        toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
//        UIBarButtonItem *finishItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(tapFinishButton)];
        UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏键盘" style:UIBarButtonItemStyleDone target:self action:@selector(tapHideKeyboardButton)];
//        [doneItem setTitleTextAttributes:@{NSFontAttributeName: [SCAppAppearance defaultTitleFontWithSize:13.0f], NSForegroundColorAttributeName: [SCAppAppearance tintColor]} forState:UIControlStateNormal];
        toolbar.items = @[spaceItem, doneItem];
        [doneItem setTintColor:[SCAppAppearance tintColor]];
        [self addSubview:toolbar];
    }
    return self;
}
- (id)initWithBiaoQingTextFieldOrTextView:(UIResponder*)textFieldOrTextView BiaoQing:(void(^)(BOOL biaoqing))biaoqingBool{
    self = [self initWithFrame:CGRectMake(0.f, 0.f, 100.f, 44.f)];
    if (self) {
        responder_ = textFieldOrTextView;
        _biaoQingOrJianPan = biaoqingBool;
        _biaoQing = NO;
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
        toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        //        UIBarButtonItem *finishItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(tapFinishButton)];
         UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
        UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"chatBar_keyboardSelected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]  style:UIBarButtonItemStyleDone target:self action:@selector(tapLeftutton:)];//chatBar_faceSelected
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:@"隐藏键盘" style:UIBarButtonItemStyleDone target:self action:@selector(tapHideKeyboardButton)];
//        [doneItem setTitleTextAttributes:@{NSFontAttributeName: [SCAppAppearance defaultTitleFontWithSize:13.0f], NSForegroundColorAttributeName: [SCAppAppearance tintColor]} forState:UIControlStateNormal];
        [doneItem setTintColor:[SCAppAppearance tintColor]];
        toolbar.items = @[spaceItem, flexSpace,doneItem];
       
        [self addSubview:toolbar];
    }
    return self;
}
- (id)initWithTextFieldOrTextViewWithFinishItem:(UIResponder *)textFieldOrTextView
{
    self = [self initWithFrame:CGRectMake(0.f, 0.f, 100.f, 44.f)];
    if (self) {
        responder_ = textFieldOrTextView;
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
        toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        UIBarButtonItem *finishItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(tapFinishButton)];
        UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        [finishItem setTitleTextAttributes:@{NSFontAttributeName: [SCAppAppearance defaultTitleFontWithSize:13.0f], NSForegroundColorAttributeName: [SCAppAppearance tintColor]} forState:UIControlStateNormal];
        toolbar.items = @[spaceItem, finishItem];
        [self addSubview:toolbar];
    }
    return self;
}

- (id)initWithTextFieldOrTextView:(UIResponder*)textFieldOrTextView title:(NSString*)title target:(id)target action:(SEL)action {
    self = [self initWithFrame:CGRectMake(0.f, 0.f, 100.f, 44.f)];
    if (self) {
        responder_ = textFieldOrTextView;
        UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.bounds];
        toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
        UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:title style:UIBarButtonItemStyleDone target:target action:action];
        [doneItem setTitleTextAttributes:@{NSFontAttributeName: [SCAppAppearance defaultTitleFontWithSize:13.0f], NSForegroundColorAttributeName: [SCAppAppearance tintColor]} forState:UIControlStateNormal];
        toolbar.items = @[spaceItem, doneItem];
        [self addSubview:toolbar];
    }
    return self;
}

- (id)initWithTextField:(UITextField*)textField {
    return [self initWithTextFieldOrTextView:textField];
}

- (id)initWithTextField:(UITextField*)textField title:(NSString*)title target:(id)target action:(SEL)action {
    return [self initWithTextFieldOrTextView:textField title:title target:target action:action];
}

- (void)tapHideKeyboardButton {
    [responder_ resignFirstResponder];
}

- (void)tapFinishButton
{
    [responder_ resignFirstResponder];
    if (self.delegate && [self.delegate respondsToSelector:@selector(finishInput)]) {
        [self.delegate finishInput];
    }
}

- (void)tapLeftutton:(UIBarButtonItem*)leftBarButton{
    _biaoQing = !_biaoQing;
    if (_biaoQing) {
        [leftBarButton setImage:[[UIImage imageNamed:@"chatBar_keyboardSelected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }else{
        [leftBarButton setImage:[[UIImage imageNamed:@"chatBar_faceSelected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    }
    self.biaoQingOrJianPan(_biaoQing);
}

@end
